package com.wuseguang.springcloud.controller;


import com.wuseguang.springcloud.Filter.RequestParameterWrapper;
import com.wuseguang.springcloud.entity.StudentEvaluationScore;
import com.wuseguang.springcloud.service.StudentEvaluationScoreService;
import com.wuseguang.springcloud.util.CodeUtil;
import com.wuseguang.springcloud.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 学生评价分数表 前端控制器
 * </p>
 *
 * @author
 * @since 2020-02-02
 */
@RestController
@RequestMapping("/studentEvaluationScore")
public class StudentEvaluationScoreController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private StudentEvaluationScoreService studentEvaluationScoreServiceImpl;


    /**
     * 查询学生评价分数
     * @param request
     * @param classId
     * @return
     */
    @RequestMapping("/findClassEvaluationScore")
    public Result findClassEvaluationScore(RequestParameterWrapper request,
                                           @RequestParam(value = "classId", required = false) Integer classId) {

        //判断班级id不能为空
        if (StringUtils.isEmpty(classId)) {
            return new Result();
        }

        //教师id
        String teacherId = request.getParam("teacherId");
        //学期id
        String termId = request.getParam("termId");

        return studentEvaluationScoreServiceImpl.findClassEvaluationScore(classId, teacherId, termId);
    }

    /**
     * 查询学生评价分数(个人表现)
     * @param request
     * @param classId
     * @return
     */
    @RequestMapping("/findClassEvaluationScoreTwo")
    public Result findClassEvaluationScoreTwo(RequestParameterWrapper request,
                                              @RequestParam(value = "classId", required = false) Integer classId,
                                              @RequestParam(value = "findType", required = false,defaultValue = "0") Integer findType,
                                              @RequestParam(value = "startTime", required = false) String startTime,
                                              @RequestParam(value = "endTime", required = false) String endTime) {

        //判断班级id不能为空
        if (StringUtils.isEmpty(classId)) {
            return new Result();
        }

        //教师id
        String teacherId = request.getParam("teacherId");

        return studentEvaluationScoreServiceImpl.findClassEvaluationScoreTwo(classId, teacherId, findType,startTime,endTime);
    }


    /**
     * 学生评分
     * @param request
     * @param studentId 学生id
     * @param scoreItemId 评分项id
     * @param name 评分项名称
     * @param fraction 分数
     * @param content 评价内容
     * @param type 评价类型 0个人，1班级，2小组
     * @param classId  班级id
     * @return
     */
    @RequestMapping("/addStudentEvaluationScore")
    public Result addStudentEvaluationScore(RequestParameterWrapper request,
                                            @RequestParam(value = "studentId", required = false) Integer [] studentId,
                                            @RequestParam(value = "scoreItemId", required = false) Integer scoreItemId,
                                            @RequestParam(value = "name", required = false) String name,
                                            @RequestParam(value = "fraction", required = false) Integer fraction,
                                            @RequestParam(value = "content", required = false) String  content,
                                            @RequestParam(value = "type",required = false)Integer type,
                                            @RequestParam(value = "classId",required = false)Integer classId,
                                            @RequestParam(value = "groupId",required = false)Integer groupId){

        logger.info("学生评分");
        //教师id
        String teacherId = request.getParam("teacherId");
        //学期id
        String termId = request.getParam("termId");
        //学校id
        String schoolId = request.getParam("schoolId");

        //评分项没有选择
        if(StringUtils.isEmpty(scoreItemId)){
            return new  Result(CodeUtil.PLEASE_CHOOSE.getCode(),CodeUtil.PLEASE_CHOOSE.getMsg()+"[评分项]");
        }
        //分数没有上传
        if (StringUtils.isEmpty(fraction)){
            return new  Result(CodeUtil.PLEASE_CHOOSE.getCode(),CodeUtil.PLEASE_CHOOSE.getMsg()+"[分数]");
        }

        //判断是个人,班级,小组
        if(type==0){
            //判断是否选择了学生
            if(StringUtils.isEmpty(studentId)){
                return new  Result(CodeUtil.PLEASE_CHOOSE.getCode(),CodeUtil.PLEASE_CHOOSE.getMsg()+"[学生]");
            }

            for (int i =0;i<studentId.length;i++) {
                //创建一个学生评分对象
                StudentEvaluationScore ses = new StudentEvaluationScore();
                ses.setStudentId(studentId[i]);
                ses.setAppraiserId(Integer.valueOf(teacherId));
                ses.setContent(content);
                ses.setFraction(fraction);
                ses.setName(name);
                ses.setScoreItemId(scoreItemId);
                ses.setTermId(Integer.valueOf(termId));
                ses.setType(0);
                ses.setSchoolId(Integer.valueOf(schoolId));
                Integer result= studentEvaluationScoreServiceImpl.personal( ses);
                if(result==2){
                    return new Result(CodeUtil.INSUFFICIENT_SCORE);
                }
            }

            return new Result();
        }else if(type==1){


            //判断是否选择了班级
            if(StringUtils.isEmpty(classId)){
                return new  Result(CodeUtil.PLEASE_CHOOSE.getCode(),CodeUtil.PLEASE_CHOOSE.getMsg()+"[班级]");
            }

            return studentEvaluationScoreServiceImpl.classes(scoreItemId,name,fraction,content,teacherId,termId,classId,Integer.valueOf(schoolId));

        }else  if(type==2){

            if(StringUtils.isEmpty(groupId)){
                return new  Result(CodeUtil.PLEASE_CHOOSE.getCode(),CodeUtil.PLEASE_CHOOSE.getMsg()+"[小组]");
            }
            return studentEvaluationScoreServiceImpl.group(scoreItemId,name,fraction,content,teacherId,termId,groupId,Integer.valueOf(schoolId));
        }



        return new Result(CodeUtil.PLEASE_CHOOSE.getCode(),CodeUtil.PLEASE_CHOOSE.getMsg()+"[评分类型]");
    }

}

